package com.gopay.bis.spdb.ebank.operation;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.gopay.bis.common.util.XMLBeanUtils;
import com.gopay.bis.spdb.ebank.operation.parse.Packet;
import com.gopay.common.domain.bank.spdb.ebank.SpdbMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 解析查询返回对象报文xml
 * 
 * @SpdbParseMessage.java
 * @author wanggang
 * @2013-12-5 上午11:31:53 www.gopay.com.cn Inc.All rights reserved.
 */
public class SpdbB2bParseMessage {

    /** 验签实现类 */
    private ICertHelper spdbB2bCertHelper;

    /**
     * @param receivedMsgData
     * @return
     * @throws GopayException
     */
    public SpdbMessage parseSpdbMessage(Object receivedMsgData, SpdbMessage spdbMsg) throws GopayException {

        String xmlMsg = (String) receivedMsgData;

        @SuppressWarnings("rawtypes")
        Map<String, Class> cm = new HashMap<String, Class>();
        cm.put("packet", Packet.class);
        Packet pack = (Packet) XMLBeanUtils.xml2Bean(cm, xmlMsg);
        String plainMessage = pack.getPlain();
        String signMessage = pack.getSignature();
        if (plainMessage == null || "".equals(plainMessage.trim()) || signMessage == null || "".equals(signMessage.trim())) {
            // 若明文和密文字段为空，则查询交易失败
            spdbMsg.setErrorCode(pack.getErrorCode());
            spdbMsg.setErrorMsg(pack.getErrorMsg());
        } else {
            // 若取得明文和密文字段，则进行验签
            spdbMsg.setPlainMessage(plainMessage);
            spdbMsg.setSignMessage(signMessage);
            if ((Boolean) this.spdbB2bCertHelper.verify(spdbMsg)) {
                spdbMsg = getSpdbMessage(pack);
            } else {
                throw new GopayException("SPDB订单查询结果验签失败！！");
            }
        }

        return spdbMsg;
    }

    /**
     * 根据解析出来的packet组装返回checkbill的SpdbMessage
     * 
     * @param pack
     * @return
     */
    private SpdbMessage getSpdbMessage(Packet pack) {
        SpdbMessage spdbMsg = new SpdbMessage();
        Map plainMap = str2map(pack.getPlain());
        spdbMsg.setTermSsn((String) plainMap.get("TermSsn"));
        spdbMsg.setAmount(new BigDecimal((String) plainMap.get("TranAmt")));
        spdbMsg.setCompFlag((String) plainMap.get("CompFlag"));
        spdbMsg.setRespCode((String) plainMap.get("RespCode"));
        spdbMsg.setSettDate((String) plainMap.get("SettDate"));
        spdbMsg.setAcqSsn((String) plainMap.get("AcqSsn"));
        return spdbMsg;
    }

    /**
     * 解析明文放入Map.
     * 
     * @param dataStr
     * @return
     */
    private Map str2map(String dataStr) {
        if (dataStr == null || dataStr.length() == 0) {
            return null;
        }
        LinkedHashMap retMap = new LinkedHashMap();
        String terms[] = dataStr.split("\\|");
        int len = terms.length;
        for (int i = 0; i < len; i++) {
            String term = terms[i];
            int index = term.indexOf("=");
            retMap.put(term.substring(0, index), term.substring(index + 1));
        }
        return retMap;
    }

    public void setSpdbB2bCertHelper(ICertHelper spdbB2bCertHelper) {
        this.spdbB2bCertHelper = spdbB2bCertHelper;
    }

}
